package com.rockcent.mall.controller;

import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.common.WebDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.Routes;
import com.rockcent.mall.lib.domain.ArticleCategory;
import com.rockcent.mall.lib.domain.enumclass.ArticleCategoryType;
import com.rockcent.mall.lib.dto.ArticleCategoryDto;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.mall.lib.factory.ArticleFactory;
import com.rockcent.mall.lib.helper.BaseUserHelper;
import com.rockcent.mall.lib.repository.ArticleCategoryRepository;
import com.rockcent.mall.lib.repository.ArticleRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Antinomy on 16/8/22.
 */
@RestController
public class ArticleCategoryController extends BaseController {

    private final Logger log = LoggerFactory.getLogger(ArticleCategoryController.class);

    @Autowired
    private BaseUserHelper userHelper;

    @Autowired
    private ArticleCategoryRepository categoryRepository;


    /**
     *
     * @api {get} /mall/article/category/list   文章分类列表
     * @apiGroup mall_helpCenter
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain    请求域名
     * @apiParam {Long} parentId 父分类Id
     * @apiSuccessExample {json} Success-Response:
     * {
     *"code": "1",
     *"desc": "成功",
     *"data": [
     *{
     *  "category": {
     *      "id": 28,
     *      "dateCreated": "2016-09-09 15:18:42",
     *      "lastUpdated": "2016-09-29 18:08:00",
     *      "version": 11,
     *      "name": "商家入驻",
     *      "displayOrderId": 2,
     *      "mallId": 1,
     *      "type": "BUSINESS_HELP_CENTER"
     *      },
     *  "subCategories": [
     *      {
     *          "id": 29,
     *          "dateCreated": "2016-09-09 15:19:13",
     *          "lastUpdated": "2016-09-09 15:19:13",
     *          "version": 0,
     *          "name": "如何注册",
     *          "displayOrderId": 1,
     *          "mallId": 1,
     *          "parentId": 28,
     *          "type": "BUSINESS_HELP_CENTER"
     *      }
     *  ]
     *}
     *]
     *}
     *
     */
    @RequestMapping(value = Routes.MALL_ARTICLE_CATEGORY_LIST, method = RequestMethod.GET)
    public JsonResult list(HttpServletRequest request, Long parentId) {
        UserInfoDto userInfoDto = userHelper.getSession(request);
        RestDoing doing = result -> {

            Long mallId = userInfoDto.mall.getId();

            List<ArticleCategory> categorysList = categoryRepository.findAllByMallIdAndIsDeleteAndParentId(
                    mallId, Whether.NO, parentId
            );

            List resultMap = new ArrayList<>();

            for (ArticleCategory category : categorysList) {
                HashMap item = new HashMap();
                item.put("category", category);

                List<ArticleCategory> categorys_2nd = categoryRepository.findAllByMallIdAndIsDeleteAndParentId(
                        mallId, Whether.NO, category.getId()
                );

                item.put("subCategories", categorys_2nd);

                resultMap.add(item);
            }

            result.data = resultMap;
        };

        return doing.go(request, log);
    }


}
